<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        小区管理
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/css/main.css" media="all">
    <script type="text/javascript" src="/lib/loading/okLoading.js"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div id="app">
    <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>小区管理</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <el-form :model="home" :rules="rules" ref="homeyFormRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="所属小区" prop="communityName">
                <el-select v-model="communityVo" placeholder="请选择所属小区" value-key="id">
                    <el-option
                            v-for="community in communityList"
                            :key="community.id"
                            :label="community.name"
                            :value="community">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="栋数" >
                <el-select prop="buildingName" v-model="buildingVo" placeholder="请选择所属栋"  value-key="id">
                    <el-option
                            v-for="building in buildingList"
                            :key="building.id"
                            :label="building.name"
                            :value="building">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="房产编码" prop="code">
                <el-input v-model="home.code"></el-input>
            </el-form-item>
            <el-form-item label="房产名称" prop="name">
                <el-input v-model="home.name"></el-input>
            </el-form-item>
            <el-form-item label="户主名称" prop="ownerName">
                <el-input v-model="home.ownerName"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="telephone">
                <el-input v-model="home.telephone"></el-input>
            </el-form-item>
            <el-form-item label="房间数" prop="roomNum">
                <el-input v-model="home.roomNum"></el-input>
            </el-form-item>
            <el-form-item label="单元" prop="unit">
                <el-input v-model="home.unit"></el-input>
            </el-form-item>
            <el-form-item label="楼层" prop="floor">
                <el-input v-model="home.floor"></el-input>
            </el-form-item >
            <el-form-item label="入住时间"   prop="liveTime">
                <el-date-picker
                        v-model="home.liveTime"
                        type="datetime"
                        placeholder="选择入住时间"
                        align="right"
                        :picker-options="pickerOptions">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="描述" prop="description">
                <el-input v-model="home.description" type="textarea">
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save()" style="text-align: center">保存</el-button>
                <el-button @click="clearAll()" style="text-align: center">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<script src="js/x-layui.js" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script src="js/vue.js" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script src="js/index.js"></script>
<!--导入Axios依赖包-->
<script src="js/axios.js" charset="utf-8"></script>
<script type="text/javascript">
    let checkPhone;
    checkPhone = (rule, value, callback) => {
        const phoneReg = /^1[3|4|5|6|7|8][0-9]{9}$/
        if (!value) {
            return callback(new Error('电话号码不能为空'))
        }
        setTimeout(() => {

            if (!Number.isInteger(+value)) {
                callback(new Error('请输入数字值'))
            } else {
                if (phoneReg.test(value)) {
                    callback()
                } else {
                    callback(new Error('电话号码格式不正确'))
                }
            }
        }, 100)
    }
    var app = new Vue({
        el:"#app",
        // 手机号验证
        data:{
            title:"添加房产",
            home:{id:""},
            communityVo:{},
            communityList:{},
            buildingVo:{},
            buildingList:{},
            pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            rules: {
                communityName: [
                    { required: true, message: '请选择小区', trigger: 'blur' }
                ],
                buildingName: [
                    { required: true, message: '请选择楼栋', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入房产编码', trigger: 'blur' },
                    { max: 50, message: '长度小于50个字符', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入房产名称', trigger: 'blur' },
                    { max: 50, message: '长度小于50个字符', trigger: 'blur' }
                ],
                ownerName: [
                    { required: true, message: '请输入户主名称', trigger: 'blur' },
                    { min: 1,max: 5, message: '长度必须大于1、小于5个字符', trigger: 'blur' }
                ],
                telephone: [
                    { required: true, message: '请输入联系方式', trigger: 'blur' },
                    { validator: checkPhone, message: '联系方式格式不正确', trigger: 'blur' }
                ],
                roomNum: [
                    { required: true, message: '请输入房间数', trigger: 'blur' }
                ],
                unit: [
                    { required: true, message: '请输入单元', trigger: 'blur' },
                ],
                floor:[
                    { required: true, message: '请输入楼层', trigger: 'blur' }
                ],
                liveTime:[
                    { required: true, message: '请输入入住时间', trigger: 'blur' }
                ],
                description:[
                    { required: true, message: '请输入描述', trigger: 'blur' }
                ],
            }
        },
        methods:{
            findById(){
                axios.get("/home/findById?id="+this.home.id).then((res)=>{
                    this.home = res.data.data;
                });
            },
            findCommunity(){
                axios.get("/community/find").then((res)=>{
                    this.communityList = res.data.data;
                });
            },
            findBuilding(){
                axios.get("/building/find").then((res)=>{
                    this.buildingList = res.data.data;
                });
            },
            //表达重置
            clearAll() {
                this.$refs.homeFormRef.resetFields();
            },
            save(){
                var method = "";
                if(this.home.id != null){
                    method = "update"
                }else{
                    method = "add"
                }
                this.home.communityId=this.communityVo.id;
                this.home.communityName=this.communityVo.name;
                this.home.buildingId=this.buildingVo.id;
                this.home.buildingName=this.buildingVo.name;
                axios.post("/home/"+method,this.home).then((res)=>{
                    if(res.data.flag){
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                });
            },
            getQueryString(name){
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
        },
        created:function(){
            layui.use(['element','layer','form'], function(){
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数，小区ID
            this.home.id = this.getQueryString("id");
            //根据小区ID获取小区对象，进行数据回显
            if(this.home.id != null && this.home.id !== ''){
                this.title="修改房产";
                this.findById();
            }
            this.findCommunity();
            this.findBuilding();
        }
    });
</script>
</body>
</html>